package com.dong.selector;

import com.dong.bean.RainBow;
import com.dong.config.T023ImportConfig;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 *  用于测试 Import 方式向容器注册Bean   方式3
 * @see T023ImportConfig
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata  当前类的注解信息
     * @param registry                                  BeanDefinition注册类  ,把所有需要注册的bean ,
     *                                                  调用该对象的注册方法祖册到bean定义注册器中. registry.registerBeanDefinition
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean hasRed = registry.containsBeanDefinition("com.com.dong.bean.Red");
        boolean hasBlue = registry.containsBeanDefinition("com.com.dong.bean.Blue");
        if(hasBlue&&hasRed) {
            BeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class) ;
            registry.registerBeanDefinition("rainBow",beanDefinition);
        }
    }
}
